package com.logic;

public class Gamelogic {
    //定义棋子角色
    public int role = 1;
    //定义棋盘数组
    public int [][] ground = new int[15][15];
    //规矩
    public boolean playChest(int r,int c) {
        //行超出范围，不落子
        if (r >= 15 || r < 0) { 
            return false;
        }
        //列超出范围，不落子
        if (c >= 15 || c < 0) {
            return false;
        }
        //有棋子的地方，不落子
        if (ground[r][c] != 0) {
            return false;
        }
        //符合规矩，可落子
        ground[r][c] = role;
        return true;
    }
    //判断
    //横向
    public boolean heng(int r,int c) {
        int offdet = 1;
        int count = 1;
        while (true) {
            if (c - offdet >= 0 && ground[r][c - offdet] == role) {
                count++;
                offdet++;
            } else {
                break;
            }
        }
        offdet = 1;
        while (true) {
            if (c + offdet < 15 && ground[r][c + offdet] == role) {
                count++;
                offdet++;
            } else {
                break;
            }
        }
        return count >= 5;
    }
    //纵向
    public boolean shu(int r, int c){
        int offdet = 1;
        int count = 1;
        while (true){
            if (r - offdet >= 0 && ground[r - offdet][c] == role){
                count++;
                offdet++;
            }else {
                break;
            }
        }
        offdet = 1;
        while (true){
            if (r + offdet < 15 && ground[r + offdet][c] == role){
                count++;
                offdet++;
            }else {
                break;
            }
        }
        return count >= 5;
    }
    //左斜
    public boolean zuoxie(int r, int c){
        int offdet = 1;
        int count = 1;
        while (true){
            if (r - offdet >= 0 && c - offdet >= 0 && ground[r - offdet][c - offdet] == role){
                count++;
                offdet++;
            }else {
                break;
            }
        }
        offdet = 1;
        while (true){
            if (r + offdet < 15 && c + offdet >= 0&& ground[r + offdet][c + offdet] == role){
                count++;
                offdet++;
            }else {
                break;
            }
        }
        return count >= 5;
    }
    //右斜
    public boolean youxie(int r, int c){
        int offdet = 1;
        int count = 1;
        while (true){
            if (r + offdet < 15 && c - offdet >= 0 && ground[r + offdet][c - offdet] == role){
                count++;
                offdet++;
            }else {
                break;
            }
        }
        offdet = 1;
        while (true){
            if (r - offdet <= 0 && c + offdet < 15 && ground[r - offdet][c + offdet] == role){
                count++;
                offdet++;
            }else {
                break;
            }
        }
        return count >= 5;
    }
    //判断游戏是否胜利
    public boolean iswinner(int r, int c){
        return heng(r, c) || shu(r, c) || zuoxie(r, c) || youxie(r, c);
    }
}
